package aufgaben210311;

public class Lock {
	int zaehler;
	boolean locked;
	long threadId = 0;

	public Lock() {
		zaehler = 0;
		locked = false;
	}

	public void lock() throws InterruptedException {
		if (locked) {
			if (Thread.holdsLock(this)) {
				zaehler++;
			} else {
				synchronized (this) {
					threadId = Thread.currentThread().getId();
					zaehler = 1;
					locked = true;
				}
			}
		} else
			wait();
	}

	public void unlock() {
		if(Thread.holdsLock(this))
		{
			locked = false;
			
		}
	}
}
